<?php

namespace app\controller\admin;

use app\BaseController;
use app\Code;
use app\dao\ClubMemberDao;
use app\model\ClubMemberModel;
use app\service\ClubService;
use app\validate\ClubValidate;
use think\exception\ValidateException;
use think\facade\Request;
use think\facade\Log;

class ClubController extends BaseController
{
    protected $service;

    public function __construct()
    {
        $this->service = new ClubService();
    }

    // 获取所有社团
    public function index()
    {
        try {
            $clubs = $this->service->getAllClubs();
            return $this->returnJson($clubs);
        } catch (\Exception $e) {
            Log::error("获取社团列表失败: " . $e->getMessage());
            return $this->returnJson([], Code::SERVER_ERROR, '服务器错误，请稍后再试');
        }
    }

    // 获取社团详情
    public function read($id)
    {
        try {
            $club = $this->service->getClubDetails($id);
            return $this->returnJson($club);
        } catch (\Exception $e) {
            Log::error("获取社团详情失败: " . $e->getMessage());
            return $this->returnJson([], Code::SERVER_ERROR, '服务器错误，请稍后再试');
        }
    }

    // 创建社团
    public function save()
    {
        $data = Request::post();

        try {
            validate(ClubValidate::class)->scene('create')->check($data);
            $club = $this->service->createClub($data);
            return $this->returnJson($club);
        } catch (ValidateException $e) {
            return $this->returnJson([], Code::BAD_REQUEST, $e->getMessage());
        } catch (\Exception $e) {
            Log::error("创建社团失败: " . $e->getMessage());
            return $this->returnJson([], Code::SERVER_ERROR, '服务器错误，请稍后再试');
        }
    }

    // 更新社团
    public function update($id)
    {
        $data = Request::put();

        try {
            validate(ClubValidate::class)->scene('update')->check($data);
            $result = $this->service->updateClub($id, $data);
            return $this->returnJson($result);
        } catch (ValidateException $e) {
            return $this->returnJson([], Code::BAD_REQUEST, $e->getMessage());
        } catch (\Exception $e) {
            Log::error("更新社团失败: " . $e->getMessage());
            return $this->returnJson([], Code::SERVER_ERROR, '服务器错误，请稍后再试');
        }
    }

    // 删除社团
    public function delete($id)
    {
        try {
            $result = $this->service->deleteClub($id);
            return $this->returnJson($result);
        } catch (\Exception $e) {
            Log::error("删除社团失败: " . $e->getMessage());
            return $this->returnJson([], Code::SERVER_ERROR, '服务器错误，请稍后再试');
        }
    }
    public function assignMember()
    {
        $data = Request::post();

        try {
            $result = (new \app\dao\ClubMemberDao)->addMember($data);
            return $this->returnJson($result);
        } catch (ValidateException $e) {
            return $this->returnJson([], Code::BAD_REQUEST, $e->getMessage());
        } catch (\Exception $e) {
            return $this->returnJson([], Code::SERVER_ERROR, $e->getMessage());
        }
    }
    public function removeMember($clubId, $userId)
    {
        try {
            $result = (new \app\dao\ClubMemberDao)->removeMember($clubId, $userId);
            return $this->returnJson(['success' => $result]);
        } catch (\Exception $e) {
            return $this->returnJson([], Code::SERVER_ERROR, $e->getMessage());
        }
    }
    public function members($clubId)
    {
        try {
            $members = (new \app\dao\ClubMemberDao)->getClubMembers($clubId);
            return $this->returnJson($members);
        } catch (\Exception $e) {
            return $this->returnJson([], Code::SERVER_ERROR, $e->getMessage());
        }
    }
}
